package ucadminlogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
)

type CreateMarqueeConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateMarqueeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateMarqueeConfigLogic {
	return &CreateMarqueeConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建活动设置
func (l *CreateMarqueeConfigLogic) CreateMarqueeConfig(in *pb.CreateMarqueeConfigReq) (*pb.CreateMarqueeConfigResp, error) {
	defer utils.RecoverPanic()

	playStartDate, err := utils.ParseTimeByString(in.PlayStartDate, dbkey.DateFormat)
	playEndDate, err := utils.ParseTimeByString(in.PlayEndDate, dbkey.DateFormat)
	if err != nil {
		return nil, xerr.NewGrpcErrCodeMsg(xerr.ParseTimeError, err.Error())
	}
	l.Logger.Infof("playStartDate: %+v, playEndDate: %+v, PlayTimes: %+v", playStartDate, playEndDate, in.PlayTimes)
	uf := usercenter.MarqueeConfig{
		MarqueeNo:      in.MarqueeNo,
		MarqueeContent: in.MarqueeContent,
		MarqueeSpeed:   in.MarqueeSpeed,
		PlayStartDate:  *playStartDate,
		PlayEndDate:    *playEndDate,
		PlayTimes:      in.PlayTimes,
		Enable:         in.Enable,
	}
	l.Logger.Infof("CreateMarqueeConfig. uf : %+v", uf)
	_, err = l.svcCtx.MarqueeConfigModel.Insert(l.ctx, &uf)
	if err != nil {
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.CreateMarqueeConfigResp{}, nil
}
